﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OracleClient;
using System.IO;

namespace xfsale.helper
{
    public partial class WinMain : Form
    {
        public WinMain()
        {
            InitializeComponent();
        }

        private void WinMain_Load(object sender, EventArgs e)
        {
            textBox1.Text = ConfigurationManager.AppSettings["ClientPacketSourceDir"];
            textBox3.Text = ConfigurationManager.AppSettings["ClientPacketSourcePackage"];
            textBox2.Text = ConfigurationManager.AppSettings["ServerPacketSourceDir"];
            textBox4.Text = ConfigurationManager.AppSettings["ServerPacketSourcePackage"];

        }

        private void GenerateClientPacketSource(string sourcePackage, string sourceDir)
        {
            OracleConnection conn = null;
            IDbCommand command = null;
            IDbCommand command2 = null;
            IDataReader reader = null;
            IDataReader reader2 = null;
            StreamWriter writer = null;

            try
            {
                conn = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString);
                conn.Open();

                command = conn.CreateCommand();
                command2 = conn.CreateCommand();

                command.CommandText = @"select * from XmPack";
                command.Parameters.Clear();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string packId = Convert.ToString(reader["packId"]);
                    string packName = Convert.ToString(reader["packName"]);
                    string packType = Convert.ToString(reader["packType"]);
                    string packDesc = Convert.ToString(reader["packDesc"]);
                    string serviceId = Convert.ToString(reader["serviceId"]);

                    FileInfo file = new FileInfo(sourceDir + packName + ".cs");
                    writer = file.CreateText();
                    writer.WriteLine(string.Format("using System;"));
                    writer.WriteLine(string.Format(""));
                    writer.WriteLine(string.Format("namespace {0}", sourcePackage));
                    writer.WriteLine(string.Format("{{"));
                    writer.WriteLine(string.Format("    public class {0}", packName));
                    writer.WriteLine(string.Format("    {{"));

                    command2.CommandText = @"select * from XmPackItem where packId=:packId order by itemOrder";
                    command2.Parameters.Clear();
                    command2.Parameters.Add(new OracleParameter("packId", packId));
                    reader2 = command2.ExecuteReader();
                    while (reader2.Read())
                    {
                        string itemName = Convert.ToString(reader2["itemName"]);
                        string itemType = Convert.ToString(reader2["itemType"]);
                        string itemScale = Convert.ToString(reader2["itemScale"]);
                        string itemPrecise = Convert.ToString(reader2["itemPrecise"]);
                        string itemDesc = Convert.ToString(reader2["itemDesc"]);
                        string itemOrder = Convert.ToString(reader2["itemOrder"]);
                        switch (itemType.ToUpper())
                        {
                            case "VCHAR":
                            case "CHAR":
                                itemType = "string";
                                break;

                            case "NUM":
                                itemType = "double";
                                break;

                            case "DATE":
                                itemType = "Date";
                                break;
                        }
                        itemName = getFieldName(itemName);

                        writer.WriteLine(string.Format("        public {0} {1};", itemType, itemName));
                    }
                    writer.WriteLine(string.Format("    }}"));
                    writer.WriteLine(string.Format("}}"));
                    writer.Close();
                    reader2.Close();
                }
                reader.Close();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (reader2 != null)
                {
                    reader2.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }

        }

        private void GenerateServerPacketSource(string sourcePackage, string sourceDir)
        {
            OracleConnection conn = null;
            IDbCommand command = null;
            IDbCommand command2 = null;
            IDataReader reader = null;
            IDataReader reader2 = null;
            StreamWriter writer = null;

            try
            {
                conn = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString);
                conn.Open();

                command = conn.CreateCommand();
                command2 = conn.CreateCommand();

                command.CommandText = @"select * from XmPack";
                command.Parameters.Clear();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string packId = Convert.ToString(reader["packId"]);
                    string packName = Convert.ToString(reader["packName"]);
                    string packType = Convert.ToString(reader["packType"]);
                    string packDesc = Convert.ToString(reader["packDesc"]);
                    string serviceId = Convert.ToString(reader["serviceId"]);

                    FileInfo file = new FileInfo(sourceDir + packName + ".java");
                    writer = file.CreateText();
                    writer.WriteLine(string.Format("package {0};", sourcePackage));
                    writer.WriteLine(string.Format(""));
                    //writer.WriteLine(string.Format("import java.util.Date;"));
                    //writer.WriteLine(string.Format(""));
                    if (packType == "R")
                    {
                        writer.WriteLine(string.Format("public final class {0} extends BaseRequest {{", packName));
                    }
                    else
                    {
                        writer.WriteLine(string.Format("public final class {0} extends BaseResponse {{", packName));
                    }

                    command2.CommandText = @"select * from XmPackItem where packId=:packId order by itemOrder";
                    command2.Parameters.Clear();
                    command2.Parameters.Add(new OracleParameter("packId", packId));
                    reader2 = command2.ExecuteReader();
                    while (reader2.Read())
                    {
                        string itemName = Convert.ToString(reader2["itemName"]);
                        string itemType = Convert.ToString(reader2["itemType"]);
                        string itemScale = Convert.ToString(reader2["itemScale"]);
                        string itemPrecise = Convert.ToString(reader2["itemPrecise"]);
                        string itemDesc = Convert.ToString(reader2["itemDesc"]);
                        string itemOrder = Convert.ToString(reader2["itemOrder"]);
                        switch (itemType.ToUpper())
                        {
                            case "VCHAR":
                            case "CHAR":
                                itemType = "String";
                                break;

                            case "NUM":
                                itemType = "double";
                                break;

                            case "DATE":
                                itemType = "Date";
                                break;
                        }
                        itemName = getFieldName(itemName);

                        writer.WriteLine(string.Format("    public {0} {1};", itemType, itemName));
                    }
                    writer.WriteLine(string.Format("}}"));
                    writer.Close();
                    reader2.Close();
                }
                reader.Close();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (reader2 != null)
                {
                    reader2.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }

        private String getFieldName(string itemName)
        {
            if (string.IsNullOrEmpty(itemName))
                return string.Empty;

            int i = 0;
            for (i = 0; i < itemName.Length; i++)
            {
                if (itemName[i] < 'A' || itemName[i] > 'Z')
                {
                    break;
                }
            }
            if (i - 1 == 0)
            {
                return itemName.Substring(0, i).ToLower() + itemName.Substring(i);
            }
            else if (i - 1 > 0)
            {
                return itemName.Substring(0, i-1).ToLower() + itemName.Substring(i-1);
            }
            else
            {
                return itemName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string clientPacketSourceDir = textBox1.Text.Trim();
            string clientPacketSourcePackage = textBox3.Text.Trim();
            GenerateClientPacketSource(clientPacketSourcePackage, clientPacketSourceDir);
            MessageBox.Show("生成源码成功");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string serverPacketSourceDir = textBox2.Text.Trim();
            string serverPacketSourcePackage = textBox4.Text.Trim();
            GenerateServerPacketSource(serverPacketSourcePackage, serverPacketSourceDir);
            MessageBox.Show("生成源码成功");
        }
    }
}
